package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Modify extends JFrame implements ActionListener {
	Connection con;
	JTextField Chinesetext, Englishtext, Phonetic_signtext;
	JButton button1;
	Statement sql;
	JLabel label;

	Modify(Connection con) {
		this.con = con;
		setLayout(null);
		init();

		this.setBounds(100, 150, 400, 400);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);

	}

	public void init() {
		// 三个文本编辑框 一个按钮 一个标签
		label = new JLabel();
		label.setBounds(57, 40, 300, 27);
		label.setText("old values-new values,以\"-\"分割");
		Chinesetext = new JTextField("Chinese");
		Chinesetext.setBounds(57, 79, 100, 27);
		Englishtext = new JTextField("English");
		Englishtext.setBounds(57, 134, 100, 27);
		Phonetic_signtext = new JTextField("Phonetic_signtext");
		Phonetic_signtext.setBounds(57, 200, 100, 27);

		button1 = new JButton("修改");
		button1.setBounds(200, 134, 100, 27);
		add(button1);
		add(Chinesetext);
		add(Englishtext);
		add(Phonetic_signtext);
		add(label);
		button1.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e) {

		try {
			sql = con.createStatement();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		if (button1 == e.getSource()) {

			String str1 = Chinesetext.getText().toString();
			String str2 = Englishtext.getText().toString();
			String str3 = Phonetic_signtext.getText().toString();

			try {
				int i = 0, i1 = 0, i2 = 0;
				if (!str1.equals("")) {
					String chinesestr[] = str1.split("-");
					i = sql.executeUpdate("update thesaurus set Chinese ='" + chinesestr[1] + "' where Chinese='"
							+ chinesestr[0] + "'");
					if (i == 1) {
						JOptionPane.showMessageDialog(null, "Chinese修改成功");
					} else
						JOptionPane.showMessageDialog(null, "Chinese修改失败");
				}

				if (!str2.equals("")) {
					String Englishtext[] = str2.split("-");
					i1 = sql.executeUpdate("update thesaurus set English='" + Englishtext[1] + "' where English='"
							+ Englishtext[0] + "'");
					if (i1 == 1) {
						JOptionPane.showMessageDialog(null, "English修改成功");
					} else
						JOptionPane.showMessageDialog(null, "English修改失败");
				}

				if (!str3.equals("")) {
					String Phonetic_signtext[] = str3.split("-");
					i2 = sql.executeUpdate("update thesaurus set Phonetic_sign ='" + Phonetic_signtext[1]
							+ "' where Phonetic_sign='" + Phonetic_signtext[0] + "'");
					if (i2 == 1) {
						JOptionPane.showMessageDialog(null, "Phonetic_sign修改成功");
					} else
						JOptionPane.showMessageDialog(null, "Phonetic_sign修改失败");
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}

		}

	}

}
